/*
package com.sc.server.system.sms.service;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.sc.common.exception.BusinessRuntimeException;
import com.sc.common.util.RedisUtils;
import com.sc.server.server.common.util.CodeUtils;
import com.sc.server.server.common.util.RedisUtils;
import com.sc.server.server.common.web.UserUtils;
import com.sc.server.system.auth.entity.User;
import com.sc.server.system.auth.service.IUserService;
import com.sc.server.system.common.constant.SmsPhoneConst;
import com.sc.server.system.auth.entity.User;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SmsAliyunService {

    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private IUserService userService;

*
     *     忘记密码短信验证
     * @param phone
     * @return


    public String  senderCodeMsgByForget(String phone) {
        User baseUser=userService.getUserByTelphone(phone);
        if(baseUser==null){
            throw new BusinessRuntimeException("该手机号不存在");
        }
        IAcsClient acsClient =getDefaultAcsClient();
        String code = CodeUtils.codeSix();
        //组装请求对象
        SendSmsResponse sendSmsResponse = sendSms(phone, code, acsClient,SmsPhoneConst.FORGET_TEMPCODE);
        //设置短信验证码70秒过期
        if ("OK".equalsIgnoreCase(sendSmsResponse.getCode())) {
            redisUtils.set(SmsPhoneConst.FORGET_PHONE+phone,code,SmsPhoneConst.Code_EXPIRE_TIME);
        }
        return checkSmsResponse(sendSmsResponse);
    }

*
     * 新手机号
     * @param phone
     * @return


    public String senderCodeMsgByNewPhone(String phone) {

        IAcsClient acsClient =getDefaultAcsClient();
        String code = CodeUtils.codeSix();
        //组装请求对象
        SendSmsResponse sendSmsResponse = sendSms(phone, code, acsClient,SmsPhoneConst.CHANGE_NEWPHONE_TEMPCODE);
        //设置短信验证码70秒过期
        if ("OK".equalsIgnoreCase(sendSmsResponse.getCode())) {
            redisUtils.set(SmsPhoneConst.CHANGE_NEWPHONE+phone,code,SmsPhoneConst.Code_EXPIRE_TIME);
        }
        return checkSmsResponse(sendSmsResponse);
    }

*
     * 修改手机号
     * @return


    public String senderCodeMsgByChangePhone() {
        IAcsClient acsClient =getDefaultAcsClient();
        String code = CodeUtils.codeSix();
        String userId = UserUtils.getUserid();
        User baseUser = userService.selectById(userId);
        if(StringUtils.isBlank(baseUser.getTelPhone())){
            throw  new BusinessRuntimeException("对不起，你没有绑定手机号，请联系管理员");
        }
        //组装请求对象
        SendSmsResponse sendSmsResponse = sendSms(baseUser.getTelPhone(), code, acsClient,SmsPhoneConst.CHANGE_PHONE_TEMPCODE);
        //设置短信验证码70秒过期
        if ("OK".equalsIgnoreCase(sendSmsResponse.getCode())) {
            redisUtils.set(SmsPhoneConst.CHANGE_PHONE+baseUser.getTelPhone(),code,SmsPhoneConst.Code_EXPIRE_TIME*2);
        }
        return checkSmsResponse(sendSmsResponse);
    }

*
     * 绑定邮箱发送短信验证码
     * @return


    public String senderCodeMsgByBindEmail() {
        IAcsClient acsClient =getDefaultAcsClient();
        String code = CodeUtils.codeSix();
        User baseUser =userService.getUser();
        if(StringUtils.isBlank(baseUser.getTelPhone())){
            throw  new BusinessRuntimeException("对不起，你没有绑定手机号，请联系管理员");
        }
        //组装请求对象
        SendSmsResponse sendSmsResponse = sendSms(baseUser.getTelPhone(), code, acsClient,SmsPhoneConst.CHANGE_EMAIL_TEMPCODE);
        //设置短信验证码70秒过期
        if ("OK".equalsIgnoreCase(sendSmsResponse.getCode())) {
            redisUtils.set(SmsPhoneConst.CHANGE_EMAIL_PHONE_CODE+baseUser.getTelPhone(),code,SmsPhoneConst.Code_EXPIRE_TIME);
        }
        return checkSmsResponse(sendSmsResponse);
    }

*
     * 注册短信验证码
     * @param phone
     * @return


    public String senderCodeRegister(String phone) {
        IAcsClient acsClient =getDefaultAcsClient();
        String code = CodeUtils.codeSix();
        User baseUser =userService.getUserByTelphone(phone);
        if(baseUser!=null){
            throw  new BusinessRuntimeException("对不起，该手机号已注册");
        }
        //组装请求对象
        SendSmsResponse sendSmsResponse = sendSms(phone, code, acsClient,SmsPhoneConst
                .REGISTER_PHONE_TEMPCODE);
        //设置短信验证码70秒过期
        if ("OK".equalsIgnoreCase(sendSmsResponse.getCode())) {
            redisUtils.set(SmsPhoneConst.REGISTER_PHONE_CODE+phone,code,SmsPhoneConst.Code_EXPIRE_TIME);
        }
        return checkSmsResponse(sendSmsResponse);
    }

*
     * 校验短信内容
     * @param sendSmsResponse
     * @return


    private String checkSmsResponse(SendSmsResponse sendSmsResponse) {
        if ("OK".equalsIgnoreCase(sendSmsResponse.getCode())) {
            // redisUtils.set(phone,code,80);
            return "短信发送成功";

        }else if("触发小时级流控Permits:5".equals(sendSmsResponse.getMessage())){

            throw  new BusinessRuntimeException("单个手机号每小时最多发送5条短信验证码，请一小时后再试");

        }else if("触发天级流控Permits:10".equals(sendSmsResponse.getMessage())){
            throw  new BusinessRuntimeException("单个手机号每天最多发送10条短信验证码，请一天后再试");
        }else {
            throw  new BusinessRuntimeException(sendSmsResponse.getMessage());
        }
    }
*
     * 发送短信
     * @param phone
     * @param code
     * @param acsClient
     * @param templateCode
     * @return


    private SendSmsResponse sendSms(String phone, String code, IAcsClient acsClient,String templateCode) {
        SendSmsRequest request = new SendSmsRequest();
        //使用post提交
        request.setMethod(MethodType.POST);
        //必填:待发送手机号。支持以逗号分隔的形式进行批量调用，批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式；发送国际/港澳台消息时，接收号码格式为国际区号+号码，如“85200000000”
        request.setPhoneNumbers(phone);
        //必填:短信签名-可在短信控制台中找到
        request.setSignName(SmsPhoneConst.SIGNNAME);
        //必填:短信模板-可在短信控制台中找到，发送国际/港澳台消息时，请使用国际/港澳台短信模版
        request.setTemplateCode(templateCode);
        //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
        //友情提示:如果JSON中需要带换行符,请参照标准的JSON协议对换行符的要求,比如短信内容中包含\r\n的情况在JSON中需要表示成\\r\\n,否则会导致JSON在服务端解析失败
        request.setTemplateParam("{\"code\":\""+code+"\"}");
        try {
            SendSmsResponse acsResponse = acsClient.getAcsResponse(request);
            return acsResponse;
        } catch (ClientException e) {
            throw  new BusinessRuntimeException("系统异常，请联系管理员");
        }
    }

*
     * 发送短信信息初始化
     * @return


    private IAcsClient getDefaultAcsClient() {
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        IClientProfile profile = DefaultProfile.getProfile(SmsPhoneConst.REGION, SmsPhoneConst.ACCESSKEYID,
                SmsPhoneConst.ACCESSKEYSECRET);
        try {
            DefaultProfile.addEndpoint(SmsPhoneConst.REGION, SmsPhoneConst.REGION, SmsPhoneConst.PRODUCT, SmsPhoneConst.DOMAIN);
        } catch (ClientException e) {
            throw  new BusinessRuntimeException("系统异常，请联系管理员");
        }
        return  new DefaultAcsClient(profile);
    }
}
*/
